package org.dtrd.modules.doctor.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.doctor.entity.bean.OutpatientInfo;
import org.dtrd.modules.doctor.entity.bean.OutpatientWeeklyInfo;
import org.dtrd.modules.doctor.service.IDtrdEntRdOutpatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_SAVE_OR_UPDATE;
import static org.dtrd.config.system.Constant.OutpatientStatus.ABLE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author QR
 * @since 2021-12-01 14:09:15
 */
@Api(tags = "医生业务相关")
@RestController
@RequestMapping("/201/04")
public class OutpatientController extends BaseController {

    @Autowired
    private IDtrdEntRdOutpatientService outpatientService;


    @ApiOperation("获取门诊列表")
    @GetMapping("/2010401")
    @AutoLog(value = "获取门诊列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getOutpatientList(@RequestParam Integer doctorId) {
        if (isIntParamInvalid(doctorId)) {
            return error(STATE_PARAMS_MISSING);
        }
        OutpatientWeeklyInfo info = outpatientService.getOutpatientList(doctorId);
        return Result.OK(info);
    }

    @ApiOperation("新增或修改门诊状态")
    @PostMapping("/2010402")
    @AutoLog(value = "新增或修改门诊状态", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateOutpatientStatus(@RequestBody OutpatientWeeklyInfo info){
        if (info.getDoctorId() == null){
            return error(STATE_PARAMS_MISSING);
        }
        // 如果你要修改成某个日期的时段为可预约的状态，那天的日期不能为空
        Optional<OutpatientInfo> any = info.getOutpatientInfos().stream()
                .filter(i -> (i.getAfternoonStatus().equals(ABLE.getStatus()) || i.getMorningStatus().equals(ABLE.getStatus()))
                        && StrUtil.isBlank(i.getOutpatientDate()))
                .findAny();
        if (any.isPresent()) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = outpatientService.saveOrUpdateOutpatientStatus(info);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

}
